<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/WEB-INF/jsp/common/navigationRight.jsp" %>
<%@include file="/WEB-INF/jsp/common/head.jsp" %>
<%@include file="/WEB-INF/jsp/common/navigationLeft.jsp" %>


<div class="m2-userCentermanage-con">
    <div class="m2-user-recharge">
        <h3><i class="m2-user-natnotice"></i>站内消息</h3>
    </div>
    <div class="m2-manage-search-selector">
        <ul class="m2-manageSearchsel-type">
            <li>消息状态：</li>
            <li class="m2-manageSearchsel-link"><span class="m2-manSealink"  onclick="getAll(2)">全部</span></li>
            <li class="m2-manageSearchsel-link"><span class="m2-manSealink-unsel"  onclick="getAll(0)">未读</span></li>
            <li class="m2-manageSearchsel-link"><span class="m2-manSealink-unsel"  onclick="getAll(1)">已读 </span></li>
        </ul>
        <input type="hidden" name="status" value="${status}" id="status">
    </div>
    <div class="m2-recharge-mainbody">
        <div class="m2-user-newsmanage">
            <script>
                //消息管理消息详情
                function showcontent() {
                    var m2Tit = $(".m2-user-newsmanage ul li h5");
                    m2Tit.toggle(function () {
                        $(this).parent('li').children('.m2-news-content').slideDown('fast', function () {
                            var pageheight = $(window).outerHeight();
                            var pagescroll = $(window).scrollTop();
                            var thistop = $(this).parent('li').offset().top;
                            var thisheight = $(this).parent('li').height();
                            if (pageheight + pagescroll < thistop + thisheight + 43) {
                                $('body').animate({scrollTop: thistop + thisheight + 43 - pageheight}, 300);
                            }
                        });
                        mesid = $(this).data('message');
                        var req = $(this);
                        if (req.children('i').hasClass('m2-news-firead')) {
                            $.ajax({
                                type: "POST",
                                url: "/Usercenter-Messagecontrol-readmessage",
                                data: {'id': mesid},
                                dataType: 'json',
                                success: function (d) {
                                    if (d['status'] == 1) {
                                        req.children('i').removeClass('m2-news-firead');
                                        req.children('i').addClass('m2-news-unread');
                                        if (Number($('#unread-msg-num').text()) > 0) {
                                            $('#unread-msg-num').text(Number($('#unread-msg-num').text()) - 1);
                                            if (Number($('#unread-msg-num').text()) == 0) {
                                                $('#unread-msg-num').remove();
                                            }
                                        }
                                    }
                                }
                            });
                        }

                    }, function () {
                        $(this).parent('li').children('.m2-news-content').slideUp('fast');
                    });
                    //消息状态
                    m2Tit.mouseover(function () {
                        $(this).css('color', '#bbb');
                        $(".m2-news-tit").css('color', '#0996cc');
                    });
                    m2Tit.mouseout(function () {
                        $(this).css('color', '#717171');
                        $(".m2-news-tit").css('color', '#0996cc');
                    });
                }

                function searchstatus() {
                    $(".m2-manageSearchsel-type .m2-manageSearchsel-link span").click(function () {
                        var status = $(this).data('status');
                        if ($(this).hasClass('m2-manSealink-unsel')) {
                            $(this).removeClass('m2-manSealink-unsel');
                            $(this).addClass('m2-manSealink-sel');
                            $(this).parent('.m2-manageSearchsel-link').siblings().children('.m2-manSealink-sel').addClass('m2-manSealink-unsel');
                            $(this).parent('.m2-manageSearchsel-link').siblings().children('.m2-manSealink-sel').removeClass('m2-manSealink-sel');
                        }
                        postData("/Usercenter-Messagecontrol-message_search", {'status': status}, function (d) {
                            $('.m2-news-body').remove();
                            $(d['html']).insertAfter('.m2-news-titcont');
                            showcontent();
                            stat = status;
                            cpage = 1;
                            page = d['pages'];
                            pager();
                        });
                    });
                }

                $(document).ready(function () {
                    showcontent();
                    searchstatus();
                    //消息管理状态切换
                    $(".m2-user-newsmanage h3").click(function () {
                        $(this).hide();
                        $(".m2-news-manage").show();
                        $(".m2-news-manageall").show();
                    });
                    $(".m2-news-quit").click(function () {
                        $(".m2-news-manageall").hide();
                        $(".m2-news-manage").hide();
                        $(".m2-user-newsmanage h3").show();
                    });

                });

                //消息管理 checkbox状态，全选，删除状态
                $(document).ready(function () {
                    // 全选
                    var subChk = $(".m2-news-checkall")
                    subChk.click(function () {
                        $(".m2-news-manage").prop("checked", subChk.length == subChk.filter(":checked").length ? true : false);
                    });
                    /* 批量删除 */
                    $(".m2-news-dele").click(function () {
                        // 判断是否至少选择一项
                        $('.m2-news-checkall').attr('checked', false);
                        var checkedNum = $("input[name='m2-checkbox-name']:checked").length;
                        if (checkedNum == 0) {
                            dialog("请选择至少一项！", 0);
                            return;
                        }
                        // 批量选择

                        var checkedList = new Array();
                        $("input[name='m2-checkbox-name']:checked").each(function () {

                            //$(this).parent('li').remove();
                            checkedList.push($(this).val());
                        });
                        $.ajax({
                            type: "POST",
                            url: "/Usercenter-Messagecontrol-delmessage",
                            dataType: 'json',
                            data: {'id': checkedList},
                            success: function (data) {

                                $.ajax({
                                    type: "POST",
                                    url: "/Usercenter-Messagecontrol-message_search",
                                    dataType: 'json',
                                    data: '',
                                    success: function (data) {
                                        $('.m2-news-body').remove();
                                        $(data['html']).insertAfter('.m2-news-titcont');
                                        $(".m2-news-manage").show();
                                        $(".m2-news-manageall").show();
                                        showcontent();
                                    }
                                });
                            }
                        });
                    });
                });
            </script>
            <h3><i></i>消息管理</h3>
            <div class="m2-news-manageall"><input type="checkbox" class="m2-news-checkall"/><a class="m2-news-dele"
                                                                                               href="javascript:void(0)">删除</a><a
                    class="m2-news-quit" href="javascript:void(0)">退出管理</a></div>
            <ul>
                <li class="m2-news-titcont"><h5 class="m2-news-tit">消息内容</h5><h6 class="m2-news-tit">生成时间</h6></li>
            </ul>
            <ul id="showall">
                <c:forEach items="${msgs}" var="aqbUserMsg" varStatus="row">
                    <li class="m2-news-titcont"><h5 class="m2-news-tit">${aqbUserMsg.aqbMsgcontent.content}</h5><h6 class="m2-news-tit"><fmt:formatDate value="${aqbUserMsg.aqbMsgcontent.createTime}" pattern="yyyy年MM月dd日  HH:mm:ss"/></h6></li>
                </c:forEach>
            </ul>
        </div>
        <%--* ***********************************************分页******************************************* *--%>
        <div class="page-bar" style="margin-left: 40px">
            <ul class="page-num-ul clearfix">
                <li>共${totalCount }条记录&nbsp;&nbsp; ${currentPageNo}/${totalPageCount} 页</li>
                <c:if test="${currentPageNo > 1 }">
                    <a onclick="showMsg(1)">首页</a>
                    <a onclick="showMsg(${currentPageNo -1 })">上一页</a>
                </c:if>
                <c:if test="${currentPageNo < totalPageCount }">
                    <a onclick="showMsg(${currentPageNo +1 })">下一页</a>
                    <a onclick="showMsg(${totalPageCount })">最后一页</a>
                </c:if>
                &nbsp;&nbsp;
            </ul>
            <span class="page-go-form"><label>跳转至</label>
             <input type="hidden" id="totalPageCount" value="${totalPageCount}"/>
	     <input type="text" name="inputPage" id="inputPage" class="page-key"/>页
	     <button type="button" class="page-btn"
                 onclick="jump_to(document.getElementById('inputPage').value)">GO</button>
		</span>
        </div>
        <%--*************************************************************************************************--%>
        <input type="hidden" class="m2-total-pages" value=0>
        <div class="m2-news-pages">
        </div>
        </if>
    </div>
</div>
</div>
<script type="text/javascript">
    $(function () {
        var pageIndex = parseInt(1);
        m2page();
        pager();
    });
    function m2page() {
        $('.m2-page-unsel').click(function () {
            pageIndex = parseInt($(this).index());
            $(this).siblings('.m2-page-sel').removeClass('m2-page-sel').addClass('m2-page-unsel');
            $(this).removeClass('m2-page-unsel').addClass('m2-page-sel');
            m2page()
        });

    }
    var page = '0';var cpage = 1;var stat = 2;
    function getList(index) {

        if (index > 0 && index <= page) {
            $.ajax({
                url: "usercenter-Messagecontrol-message_search",
                type: "POST",
                data: {
                    start: index - 1,
                    status: stat
                },
                success: function (data) {
                    obj = eval('(' + data + ')');
                    $('.m2-news-body').remove();
                    $(obj['html']).insertAfter('.m2-news-titcont');
                    showcontent();
                    cpage = index;
                    page = obj['pages'];
                    pager();
                }
            });
        }
    }
    function pager() {

        if (page == 0) {
            $('.m2-news-pages').hide();
        } else {
            $('.m2-news-pages').show();
            var n = 0;
            if (page < 6) {
                n = page;
            } else if (cpage < page) {
                n = cpage + 1 > 5 ? cpage + 1 : 5;
            } else {
                n = cpage;
            }
            var m = n - 4 > 0 ? n - 4 : 1;
            var pages = '<a class="m2-pages-num m2-pages-prev" onclick="getList(' + (cpage - 1) + ')">&lt;</a>';
            for (var i = m; i <= n; i++) {
                if (i == cpage) {
                    pages += '<a class="m2-pages-num m2-page-sel" onclick="getList(' + i + ')">' + i + '</a>';
                } else {
                    pages += '<a class="m2-pages-num m2-page-unsel" onclick="getList(' + i + ')">' + i + '</a>';
                }
            }
            pages += '<a class="m2-pages-num m2-pages-next" onclick="getList(' + (cpage + 1) + ')">&gt;</a>';
            $('.m2-news-pages').html(pages);
        }
    }
    /*******************************************************自己的js***********************************************/
    function getAll(status){
        $("#status").val(status)
        window.location.href="/msg/getAll?status="+status+"&currentPageNo=1";

    }

    function jump_to(jumpPageNo){
        var regx = /^\d{1,}$/;
        if(!regx.test(jumpPageNo)){//非数字
            alert("请输入数字!");
            return;
        }
        var totalPageCount = document.getElementById("totalPageCount").value;
        alert("totalPageCount:"+totalPageCount)
        if(jumpPageNo <= 0 || jumpPageNo > totalPageCount){//在首页和最后一页之间
            alert("请输入正确的页码!");
            return;
        }
        showMsg(jumpPageNo);
    }
    function showMsg(currentPageNo){
        var status=$("#status").val();
        window.location.href = "/msg/getAll?status="+status+"&currentPageNo="+currentPageNo;
    }
    /********************************************************结束**************************************************/
</script>
</body>
</html>